package edu.hawaii.myisern.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.meterware.httpunit.TableCell;
import edu.hawaii.myisern.model.Log;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests operation of the MyIsern researcher web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestOrganizationActionBeans {

  /** Get the test host. */
  private String Url = System.getProperty("test.host") + "myisern-tan";
  private String username = "test";
  private String password = "test";
  private String success = "You have logged in successfully!";

  /**
   * Test the main menu new organization button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewOrganizationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern 2.0", response.getTitle());

    // Push login button
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(request);
    String text = response.getText();
    assertTrue("Checking login", text.contains(success));

    // Click on select organization link.
    WebLink link = response.getLinkWithID("SelectOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking select organization", text.contains("List of ISERN Organizations:"));

    // Clear organizations.
    link = response.getLinkWithID("ClearOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    WebTable table = response.getTableWithID("SelectOrganizationTable");
    Integer expected = 1;
    Integer count = table.getRowCount();
    assertEquals("Organizations should have only header row", expected, count);

    // Click on new organization link.
    link = response.getLinkWithID("NewOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking new organization", text.contains("Edit ISERN Organization:"));

    // Click on save.
    form = response.getFormWithID("EditOrganizationForm");
    form.setParameter("name", "Test Organization");
    form.setParameter("contact", "Test Contact");
    form.setParameter("homePage", "Test Home Page");
    form.setParameter("country", "Test Country");
    form.setParameter("researchDescription", "Test Researcher Description");
    form.setParameter("newAffiliatedResearcher", "Test1");
    form.setParameter("newResearchKeyword", "Word1");
    form.setParameter("type", "Test Type");
    response = conversation.getResponse(form.getRequest("save"));

    // Check if new name is in select list and click it.
    table = response.getTableWithID("SelectOrganizationTable");
    TableCell cell = table.getTableCell(1, 0);
    assertNotNull("Check if cell exisits", cell);

    String contents = cell.getText();
    Log.enter("TestOrganizationActionBeans cell contents = " + contents);
    link = cell.getLinkWith("Test");
    assertNotNull("Check if link in cell exisits", link);

    response = conversation.getResponse(link.getRequest());
    form = response.getFormWithID("EditOrganizationForm");
    assertEquals("Check field.", "Test Organization", form.getParameterValue("name"));
    assertEquals("Check field.", "Test Contact", form.getParameterValue("contact"));
    assertEquals("Check field.", "Test Home Page", form.getParameterValue("homePage"));
    assertEquals("Check field.", "Test Country", form.getParameterValue("country"));
    assertEquals("Check field.", "Test Researcher Description", form
        .getParameterValue("researchDescription"));
    assertEquals("Check field.", "Test Type", form.getParameterValue("type"));

    // Cancel
    Log.enter("Click on cancel.");
    form = response.getFormWithID("EditOrganizationForm");
    response = conversation.getResponse(form.getRequest("cancel"));

  }
}
